package com.example.tellwin.mine.act;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.viewpager2.widget.ViewPager2;

import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.base.CpBaseActivty;
import com.example.tellwin.base.DaggerRxComponent;
import com.example.tellwin.common.Common;
import com.example.tellwin.event.OrderDetailEvent;
import com.example.tellwin.event.OrderListEvent;
import com.example.tellwin.mine.adapter.OrderListAdapter;
import com.example.tellwin.mine.adapter.OrderListPagerAdpater;
import com.example.tellwin.mine.bean.OrderBean;
import com.example.tellwin.mine.bean.OrderListTabBean;
import com.example.tellwin.mine.contract.OrderListContract;
import com.example.tellwin.mine.presenter.OrderListPresenter;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OrderListActivity extends CpBaseActivty implements OrderListContract.View, OrderListAdapter.OrderItemClickListener {

    @BindView(R.id.tab_layout)
    CommonTabLayout tabLayout;
    @BindView(R.id.viewpager)
    ViewPager2 viewpager;

    private OrderListPagerAdpater orderListPagerAdpater;
    String[] tabStrs = new String[]{"全部", "未接单", "已完成", "已付款", "已评价"};
    String[] tabNormalStrs = new String[]{"全部", "已取消", "已完成", "已付款", "已评价"};
    OrderListAdapter allAdapter;
    OrderListAdapter noAnswerAdapter;
    OrderListAdapter accomplishAdapter;
    OrderListAdapter paidAdapter;
    OrderListAdapter evaluatedAapter;

    @Inject
    OrderListPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initViews();
        initEvents();
        initDatas();
    }

    @Override
    protected void initViews() {

        DaggerRxComponent.builder()
                .baseAppComponent(MainApplication.getAppComponent())
                .build()
                .inject(this);
        mPresenter.attachView(this);


        orderListPagerAdpater = new OrderListPagerAdpater();
        viewpager.setAdapter(orderListPagerAdpater);

        allAdapter = new OrderListAdapter();
        noAnswerAdapter = new OrderListAdapter();
        accomplishAdapter = new OrderListAdapter();
        paidAdapter = new OrderListAdapter();
        evaluatedAapter = new OrderListAdapter();

        ArrayList<CustomTabEntity> tabEntities = new ArrayList<>();
        if (MainApplication.Identity.equals(Common.NORMAL)) {
            for (int i = 0; i < tabNormalStrs.length; i++) {
                tabEntities.add(new OrderListTabBean(tabNormalStrs[i], 0, 0));
            }
        } else {
            for (int i = 0; i < tabStrs.length; i++) {
                tabEntities.add(new OrderListTabBean(tabStrs[i], 0, 0));
            }
        }
        tabLayout.setTabData(tabEntities);
        List<OrderListAdapter> orderListAdapters = new ArrayList<>();
        orderListAdapters.add(allAdapter);
        orderListAdapters.add(noAnswerAdapter);
        orderListAdapters.add(accomplishAdapter);
        orderListAdapters.add(paidAdapter);
        orderListAdapters.add(evaluatedAapter);
        orderListPagerAdpater.setListAdapters(orderListAdapters);
    }

    @Override
    protected void initEvents() {
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                getTabOrderList(position);
                viewpager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        viewpager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                getTabOrderList(position);
                tabLayout.setCurrentTab(position);
            }
        });
        orderListPagerAdpater.setItemScrollBottomListener(position -> mPresenter.loadMore(getStatusToPosition(position)));
        allAdapter.setOrderItemClickListener(this);
        noAnswerAdapter.setOrderItemClickListener(this);
        accomplishAdapter.setOrderItemClickListener(this);
        paidAdapter.setOrderItemClickListener(this);
        evaluatedAapter.setOrderItemClickListener(this);
    }

    @Override
    protected void initDatas() {
        setTitle(R.string.mine_order);
        mPresenter.getOrderList(-1);

    }

    private void getTabOrderList(int position) {
        mPresenter.getOrderList(getStatusToPosition(position));
    }

    private int getStatusToPosition(int position) {
        int status = -1;//0待接单1已完成2已付款3已评价4已取消5不满意,不传或传null为全部
        if (MainApplication.Identity.equals(Common.NORMAL)) {
            if (position == 1) {
                status = 4;
            } else if (position == 2) {
                status = 1;
            } else if (position == 3) {
                status = 2;
            } else if (position == 4) {
                status = 3;
            } else {
                status = -1;
            }
            return status;
        } else {
            if (position == 1) {
                status = 0;
            } else if (position == 2) {
                status = 1;
            } else if (position == 3) {
                status = 2;
            } else if (position == 4) {
                status = 3;
            } else {
                status = -1;
            }
            return status;
        }

    }

    @Override
    public void orderListResult(List<OrderBean> orderBeans, int status) {
        if (MainApplication.Identity.equals(Common.NORMAL)) {
            if (status == -1) {
                allAdapter.setOrderBeans(orderBeans);
            } else if (status == 1) {
                accomplishAdapter.setOrderBeans(orderBeans);
            } else if (status == 2) {
                paidAdapter.setOrderBeans(orderBeans);
            } else if (status == 3) {
                evaluatedAapter.setOrderBeans(orderBeans);
            } else if (status == 4) {
                noAnswerAdapter.setOrderBeans(orderBeans);
            }
        } else {
            if (status == -1) {
                allAdapter.setOrderBeans(orderBeans);
            } else if (status == 0) {
                noAnswerAdapter.setOrderBeans(orderBeans);
            } else if (status == 1) {
                accomplishAdapter.setOrderBeans(orderBeans);
            } else if (status == 2) {
                paidAdapter.setOrderBeans(orderBeans);
            } else if (status == 3) {
                evaluatedAapter.setOrderBeans(orderBeans);
            }
        }
    }

    @Override
    public void loadMoreResult(List<OrderBean> orderBeans, int status) {
        if (status == -1) {
            allAdapter.addOrderBeans(orderBeans);
        } else if (status == 0) {
            noAnswerAdapter.addOrderBeans(orderBeans);
        } else if (status == 1) {
            accomplishAdapter.addOrderBeans(orderBeans);
        } else if (status == 2) {
            paidAdapter.addOrderBeans(orderBeans);
        } else if (status == 3) {
            evaluatedAapter.addOrderBeans(orderBeans);
        }
    }

    @Override
    public void onOrderItemClick(int position, OrderBean orderBean) {
        Intent intent = new Intent(this, OrderDetailActivity.class);
        intent.putExtra(Common.ORDER, orderBean.getOrderId());
        startActivityForResult(intent, Common.ORDER_LIST_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Common.ORDER_LIST_REQUEST_CODE && resultCode == Common.UPDATE_ORDER_LIST) {
            mPresenter.clearOrderTab();
            for (int i = 0; i < tabStrs.length; i++) {
                mPresenter.getOrderList(getStatusToPosition(i));
            }
        }
    }

    @Subscribe
    public void onEvent(OrderListEvent event) {
        mPresenter.clearOrderTab();
        allAdapter.clearData();
        noAnswerAdapter.clearData();
        accomplishAdapter.clearData();
        paidAdapter.clearData();
        evaluatedAapter.clearData();
        mPresenter.getOrderList(getStatusToPosition(viewpager.getCurrentItem()));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
